package com.welink.cmp.adapter.api.topotree;

import com.welink.cmp.dto.ErrorCode;
import com.welink.cmp.dto.topotree.BuildServiceTopologyAddCmd;
import com.welink.cmp.dto.topotree.GetHostByServiceTreeQry;
import com.welink.cmp.dto.topotree.ModifyServiceTopologyUptCmd;
import com.welink.cmp.dto.topotree.ServiceTreeCO;
import com.welink.cmp.topotree.ServiceTopologyAppService;
import com.welink.common.dto.MultiResponse;
import com.welink.common.dto.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务拓扑服务API
 *
 * @author yanwei li create  2024-05-24 17:34
 */
@RestController
@RequestMapping("/api/servicetopo")
public class ServiceTopologyServiceApi {

    @Autowired
    private ServiceTopologyAppService serviceTopologyAppService;

    /**
     * [新增]构建服务拓扑关系
     * @return resultCode
     */
    @PostMapping("/build")
    public Response buildServiceTopology(@RequestBody BuildServiceTopologyAddCmd addCmd){
        final Boolean b = serviceTopologyAppService.buildServiceTopology(addCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"构建Host标签失败");
        }
        return Response.buildSuccess();
    }

    /**
     * [修改]修改主机绑定服务数
     * @param updCmd 服务树标签
     * @return 成功
     */
    @PostMapping("/modifyLabel")
    public Response modifyServiceLabel(@RequestBody ModifyServiceTopologyUptCmd updCmd){
        final Boolean b = serviceTopologyAppService.modifyServiceLabel(updCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"构建Host标签失败");
        }
        return Response.buildSuccess();
    }

    /**
     * [列表查询] 通过服务标签查询 标签下机器
     * @param treeQry 标签字符串
     * @return 主机 ID 列表
     */
    @GetMapping("/get-hosts")
    public MultiResponse<ServiceTreeCO> getHostByServiceLabel(@RequestBody GetHostByServiceTreeQry treeQry){
        final List<ServiceTreeCO> hostByServiceLabel = serviceTopologyAppService.getHostByServiceLabel(treeQry);
        return MultiResponse.of(hostByServiceLabel);
    }
}
